/**
 * 
 */
package com.evon.yardmanagement.ui.action;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.validation.SkipValidation;

import com.evon.yardmanagement.model.KeyBin;
import com.evon.yardmanagement.model.KeyBox;
import com.evon.yardmanagement.service.KeyBinManagementService;
import com.evon.yardmanagement.service.KeyBoxManagementService;
import com.evon.yardmanagement.transformer.KeyBinTransformer;
import com.evon.yardmanagement.transformer.KeyBoxTransformer;
import com.evon.yardmanagement.ui.dto.KeyBinDTO;
import com.evon.yardmanagement.ui.dto.KeyBoxDTO;
import com.evon.yardmanagement.ui.dto.ParkingSlotDTO;
import com.evon.yardmanagement.ui.dto.YardDTO;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;

/**
 * @author Sandeep
 *
 */
public class AddKeyBinAction extends ActionSupport {
	
	/*private Integer keyBinId;
	
	private String keyBinName;
	
	private Integer keyBinNumber;
	
	private String keyOccupied;*/
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private KeyBinDTO keyBinDTO;
	
	private int keyBinId;
	
	private int yardId;
	
	private int oemId;
	
	private int addCounter = 1;
	
	

	public int getOemId() {
		return oemId;
	}

	@SkipValidation
	public void setOemId(int oemId) {
		this.oemId = oemId;
	}


	public int getYardId() {
		return yardId;
	}

	@SkipValidation
	public void setYardId(int yardId) {
		this.yardId = yardId;
	}

	private List<KeyBoxDTO> listKeyBoxes;
	
	public AddKeyBinAction (){
		listKeyBoxes = new ArrayList<KeyBoxDTO>();
		keyBinDTO = new KeyBinDTO();
	}
	
	
	public List<KeyBoxDTO> getListKeyBoxes() {
		return listKeyBoxes;
	}
	@SkipValidation
	public void setListKeyBoxes(List<KeyBoxDTO> listKeyBoxes) {
		this.listKeyBoxes = listKeyBoxes;
	}

	public int getKeyBinId() {
		return keyBinId;
	}
	@SkipValidation
	public void setKeyBinId(int keyBinId) {
		this.keyBinId = keyBinId;
	}

	public KeyBinDTO getKeyBinDTO() {
		return keyBinDTO;
	}
	@SkipValidation
	public void setKeyBinDTO(KeyBinDTO keyBinDTO) {
		this.keyBinDTO = keyBinDTO;
	}

	@SkipValidation
	public String  populate() throws Exception {
		if(getKeyBinId()!=0) {
			KeyBinManagementService service = new KeyBinManagementService();
			List<KeyBin> keybins = service.getKeyBinByKeyBinId(getKeyBinId());
			//SectionDTO sectionDTO = null;
			for (KeyBin keyBin : keybins) {
				keyBinDTO = KeyBinTransformer.transformKeyBinToDTO(keyBin);

			}
			
			KeyBoxManagementService kbservice = new KeyBoxManagementService();
			List<KeyBox> kbList = kbservice.fetchKeyBoxByKeyBinId(getKeyBinId());
			
				for(KeyBox keyBox: kbList) {
					listKeyBoxes.add(KeyBoxTransformer.transformToModel(keyBox));
				
			}
		}
		return "populate";
	}
	@SkipValidation
	public String addKeyBox() {
		if (keyBinDTO.getCapacity()<= listKeyBoxes.size()) {
			addActionMessage("Cannot add Key Box more then the specified capacity");
		} else {
			for (int i = 0; i < addCounter; i++) {
				listKeyBoxes.add(new KeyBoxDTO());
			}
		}
		return SUCCESS;
	}
	
	@Validations(
			requiredStrings = { 
					@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "keyBinDTO.keyBinName", message = "You must enter a character name."),
					@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "keyBinDTO.keyBinNumber", message = "You must enter a Key Bin number.")
			}
	)
	public String execute() throws Exception {
		
		/*if (!validateKeyBin()) {
			return SUCCESS;
		}*/
		//KeyBinDTO keyBinDto = new KeyBinDTO();
		/*Map map = ActionContext.getContext().getSession();
		YardDTO yardDTO = (YardDTO)map.get("yard");*/
		int yardId = getYardId();
		if (yardId!=0) {
			YardDTO yardDTO = new YardDTO();
			yardDTO.setId(yardId);
			KeyBinDTO keyBinDTO = getKeyBinDTO();
			keyBinDTO.setYardDTO(yardDTO);
			KeyBin keyBin = KeyBinTransformer.transformKeyBin(keyBinDTO);
			/*KeyBin keyBin = new KeyBin(); 
		keyBin.setId(getKeyBinId());
		keyBin.setName(getKeyBinName());
		keyBin.setBinNumber(getKeyBinNumber());
		keyBin.setIsOccupied(getKeyOccupied());
			 */
			KeyBinManagementService service = new KeyBinManagementService();
			int returnedKeyBinId = service.addKeyBin(keyBin);
			if (returnedKeyBinId >0) {
				addActionMessage("Key Bin information successfully added");
				getKeyBinDTO().setKeyBinId(returnedKeyBinId);
				if (listKeyBoxes.size()==0){
					for (int i = 0; i < getKeyBinDTO().getCapacity(); i++) {
						listKeyBoxes.add(new KeyBoxDTO());
					}
				}

			} else {
				addActionMessage("Critical Error");
			}
			
		} else {
			System.out.println("Yarddto is null");
		}
		
		
		
		return SUCCESS;
	}
	
	private boolean validateKeyBin() {
		boolean success = true;
		KeyBinDTO keyBinDTO = getKeyBinDTO();
		if (keyBinDTO!=null && (keyBinDTO.getKeyBinName()==null || "".equals(keyBinDTO.getKeyBinName()))) {
			addActionMessage("Key Bin name cannot be left empty");
			success = false;
		}
		
		if (keyBinDTO!=null && (keyBinDTO.getKeyBinNumber()==null || "".equals(keyBinDTO.getKeyBinNumber()))) {
			addActionMessage("Key Bin number cannot be left empty");
			success = false;
		}
		return success;
	}
	
	public String submitKeyBoxes() throws Exception {

		KeyBoxManagementService service = new KeyBoxManagementService();
		boolean success =  service.addKeyBoxes(listKeyBoxes, keyBinDTO.getKeyBinId());
		if (success) {
			addActionMessage("Key Box information successfully added");
		} else {
			addActionMessage("Critical Error");
		}
		return SUCCESS;
	}

	/*public Integer getKeyBinId() {
		return keyBinId;
	}

	public void setKeyBinId(Integer keyBinId) {
		this.keyBinId = keyBinId;
	}

	public String getKeyBinName() {
		return keyBinName;
	}

	public void setKeyBinName(String keyBinName) {
		this.keyBinName = keyBinName;
	}

	public Integer getKeyBinNumber() {
		return keyBinNumber;
	}

	public void setKeyBinNumber(Integer keyBinNumber) {
		this.keyBinNumber = keyBinNumber;
	}

	public String getKeyOccupied() {
		return keyOccupied;
	}

	public void setKeyOccupied(String keyOccupied) {
		this.keyOccupied = keyOccupied;
	}
*/
}
